package com.nari.horizontalselectviewlib;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class MyHSVActivity extends Activity implements AdapterView.OnItemClickListener, HorizontalScrollSelectView.OnScrollListener {
    HorizontalScrollSelectView horizontalScrollSelectView;
    EditText editText;
    View view1;
    private String[] textColor = new String[]{"#000000", "#7F7F7F",
            "#880016", "#ED1C24", "#FF7F27", "#FFF200", "#23B14D", "#00A2E8",
            "#3F48CC", "#A349A4", "#FFFFFF"};

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, MyHSVActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_font_size);
        view1 = findViewById(R.id.view1);
        horizontalScrollSelectView = (HorizontalScrollSelectView) findViewById(R.id.select_view);
        editText = (EditText) findViewById(R.id.edit_font);
        horizontalScrollSelectView.setAdapter(new ImageAdapter());
        horizontalScrollSelectView.setOnItemClickListener(this);
        horizontalScrollSelectView.setMScrollListener(this);
        view1.getLayoutParams().width = DensityUtils.getSecreenWidth(this) / 5;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onScrollSelectItem(ViewGroup viewGroup, int position) {
        editText.setTextColor(Color.parseColor(textColor[position]));
    }

    public class ImageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (textColor != null && textColor.length > 0) {
                return textColor.length;
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {

            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv = null;
            if (convertView == null) {
                LinearLayout li = new LinearLayout(MyHSVActivity.this);
                li.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.FILL_PARENT));
                LinearLayout li1 = new LinearLayout(MyHSVActivity.this);
                li1.setLayoutParams(new ViewGroup.LayoutParams(DensityUtils
                        .getSecreenWidth(MyHSVActivity.this) / 5,
                        DensityUtils.dp2px(MyHSVActivity.this, 50)));
                li1.setGravity(Gravity.CENTER);
                li1.setPadding(20, 0, 20, 0);
                iv = new ImageView(MyHSVActivity.this);
                iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                        30));

                li1.addView(iv);
                li.addView(li1);
                convertView = li;
                convertView.setTag(iv);
            }
            iv = (ImageView) convertView.getTag();

            iv.setBackgroundColor(Color.parseColor(textColor[position]));
            return convertView;
        }

    }
}
